﻿Ext.ns('fifachamps');

fifachamps.core = {
    username: "2",
    password: "",
    access: [],
    tabpanel: {
        get_tabpanel: function() { 
            return Ext.getCmp('TABPNL_main');
        },
        add_tab: function(item) {
            var tab = this.get_tabpanel();

            if (item && item != 'undefined')
            {
                tab.add(item);
                tab.setActiveTab(item.id);
                tab.doLayout();
            }
        }
    },
    initialize: function() {
        var tab = new Ext.TabPanel({
            id: 'TABPNL_main',
            renderTo: 'tabpanel',
            height: 800
        });

        Ext.each(this.access, function(item, index) {
            fifachamps[item].initialize();
        });
    },
    post: function(cmd, params, success_callback, failure_callback, error_callback, url) {
        var req_params = { cmd: cmd };

        if (params) req_params = Ext.apply(req_params, params);

        Ext.Ajax.request({
            url: 'Proxy.ashx',
            reader: 'json',
            success: success_callback,
            faliure: failure_callback,
            params: req_params
        });
    }, 
    base_store: function (Params) {
            Ext.define('MDL_' + Params.StoreID, {
                 extend: 'Ext.data.Model',
                 fields: Params.Fields
             });

            var constructor = {
                storeId: Params.StoreID,
                model: 'MDL_' + Params.StoreID,
                                
                proxy: {
                    type: 'ajax',
                    url: Params.Proxy,
                    reader: {
                        type: 'json',
                        root: Params.Root
                    }
                }
            }

            if (Params.PageSize) { 
                constructor = Ext.apply(constructor, { pageSize: Params.PageSize });
            }

            if (Params.AutoLoad) {
                constructor = Ext.apply(constructor, Params.AutoLoad);
            }

            var store = new Ext.data.Store(constructor);

            if (Params.Load != null && Params.Load) {
                store.load({
                    params: Params.LoadParams,
                    callback: (Params.LoadCallBack != null) ? Params.LoadCallBack : null
                });
            }

            return store;
        }
}

Ext.onReady(function () {
    fifachamps.core.initialize();
});